package org.edu.modules.stu.zhsz.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.edu.common.dto.ExcelPo;
import org.edu.common.utils.PoiUtils;
import org.edu.modules.stu.zhsz.dto.VeStuCheckNormDto;
import org.edu.common.dto.AllDto;
import org.edu.modules.stu.zhsz.entity.VeStuCheckNorm;
import org.edu.modules.stu.zhsz.service.VeStuCheckNormService;
import org.edu.common.api.vo.Result;
import org.edu.common.base.PageDto;
import org.edu.common.dto.StatusDto;
import org.edu.common.aspect.annotation.AutoLog;
import org.edu.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Api(tags = {"检查指标表"})
@RestController
@RequestMapping("/stu/veStuCheckNorm")
@Slf4j
public class VeStuCheckNormController {

    @Autowired
    private VeStuCheckNormService veStuCheckNormService;

    @ApiOperation("分页查询检查指标表")
    @PostMapping("/page")
    public Result<?> page(@RequestBody PageDto<VeStuCheckNorm> page) {
        QueryWrapper<VeStuCheckNorm> queryWrapper = page.buildQueryWrapper(VeStuCheckNorm.class);
        return Result.OK(veStuCheckNormService.page(page, queryWrapper));
    }

    /**
     * 查询所有
     */
    @ApiOperation("查询所有")
    @PostMapping("/all")
    public Result<?> all(@RequestBody AllDto<VeStuCheckNorm> page) {
        QueryWrapper<VeStuCheckNorm> queryWrapper = page.buildQueryWrapper(VeStuCheckNorm.class);
        if (page.getParams() != null && page.getParams().containsKey("itemId")) {
            queryWrapper.apply("  id in( select norm_id from ve_stu_check_item_norm where item_id={0} )  ",
                    page.getParams().get("itemId"));
        }
        List<VeStuCheckNorm> list = veStuCheckNormService.listByWrapper(queryWrapper);
        return Result.OK(list);
    }

    @ApiOperation("导出数据）")
    @PostMapping("/export")
    public Result<?> export(@RequestBody PageDto<VeStuCheckNorm> page, HttpServletResponse resp) {
        QueryWrapper<VeStuCheckNorm> queryWrapper = page.buildQueryWrapper(VeStuCheckNorm.class);
        List<ExcelPo> list = new ArrayList<>();
        list.add(new ExcelPo("指标类型", "type"));
        list.add(new ExcelPo("指标名称", "name"));
        list.add(new ExcelPo("指标描述", "description"));
        list.add(new ExcelPo("排序字段", "listSort"));
        list.add(new ExcelPo("父Id", "pid"));
        list.add(new ExcelPo("路径", "path"));
        PoiUtils.downloadFile(resp, veStuCheckNormService.page(page, queryWrapper).getRecords(), list);
        return Result.OK();
    }

    @AutoLog(value = "检查指标表-分页列表查询")
    @ApiOperation(value = "检查指标表-分页列表查询", notes = "检查指标表-分页列表查询")
    @PostMapping(value = "/list")
    public Result<?> queryPageList(VeStuCheckNorm entity,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<VeStuCheckNorm> queryWrapper = QueryGenerator.initQueryWrapper(entity, req.getParameterMap());
        PageDto<VeStuCheckNorm> page = new PageDto<VeStuCheckNorm>(pageNo, pageSize);
        IPage<VeStuCheckNorm> pageList = veStuCheckNormService.page(page, queryWrapper);
        return Result.OK(pageList);
    }


    @AutoLog(value = "通过id查询")
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        VeStuCheckNorm dto = veStuCheckNormService.findDtoById(id);
        if (dto == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(dto);
    }

    @AutoLog(value = "添加")
    @ApiOperation(value = "添加", notes = "添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody VeStuCheckNormDto dto) {
        veStuCheckNormService.insertOrUpdate(dto, true);
        return Result.OK("添加成功！");
    }

    @AutoLog(value = "编辑")
    @ApiOperation(value = "编辑", notes = "编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody VeStuCheckNormDto dto) {
        veStuCheckNormService.insertOrUpdate(dto, false);
        return Result.OK("编辑成功!");
    }

    @AutoLog(value = "批量删除")
    @ApiOperation(value = "批量删除", notes = "批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        veStuCheckNormService.delete(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }


    @AutoLog(value = "更新状态")
    @ApiOperation(value = "更新状态")
    @PostMapping(value = "/updateStatus")
    public Result<?> updateStatus(@RequestBody StatusDto statusDto) {
        LambdaUpdateWrapper<VeStuCheckNorm> updateWrapper = new LambdaUpdateWrapper<>();
        if (statusDto.getIds() != null && statusDto.getIds().size() > 0) {
            //     updateWrapper.set(VeStuCheckNorm::getStatus,statusDto.getStatus());
            updateWrapper.in(VeStuCheckNorm::getId, statusDto.getIds());
            veStuCheckNormService.update(updateWrapper);
            return Result.OK("批量更新成功!");
        } else {
            return Result.error("未选择!");
        }
    }


}
